package com.itheima.health.util;

import java.util.Random;

public class ValidateCodeUtils {
    /**
     * 随机生成验证码
     * @param length 长度为4位或者6位
     * @return
     */
    public static Integer generateValidateCode(int length){
        //做一个随机数长度为4或者6的位数
        Integer code = null;
        if (length == 4) {
            code = new Random().nextInt(9999);//最大数为9999
            if (code < 1000){//当随机数小于1000时
                code = code + 1000;
            }
        }else if (length == 6 ){
            code = new Random().nextInt(999999);//最大数为9999999
            if (code < 1000000){//当随机数小于100000时
                code = code + 100000;
            }
        }else {
            throw new RuntimeException("只能是一个4位数或着6位数的数");
        }
        return code;
    }
}
